﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TimeTrak.BLL
{
    public class MainTaskHolderThing
    {

        public BindingTaskList TaskList { get; set; }
        int maxID;
        private const TimeUnitType DEFAULT_TIME_UNIT = TimeUnitType.Hour;

        internal void AddNewTask()
        {
            maxID++;
            TaskList.Add(new Task(maxID, "", false, false, 0m, DEFAULT_TIME_UNIT, new List<TimeRecord>(), "", 5));

        }

        internal void DeleteTask(int id)
        {
            foreach (Task item in TaskList)
            {
                if (item.ID == id)
                {
                    TaskList.Remove(item);
                    break;
                }
            }
        }

        public TimeSpan GetHoursForDate(DateTime date)
        {
            TimeSpan total = new TimeSpan();
            foreach (Task item in TaskList)
            {
                foreach (TimeRecord rec in item.TimeRecordList)
                {
                    //record was entierly in this date
                    if (rec.StartTime.Date == date && rec.EndTime.Date == date)
                    {
                        total = total.Add(rec.GetDuration());
                    }
                    //record started before the date but ended during the date
                    else if (rec.StartTime.Date < date && rec.EndTime.Date == date)
                    {
                        //get the part that was during the date
                        TimeSpan temp = rec.EndTime.Subtract(date);
                        total = total.Add(temp);
                    }
                    //record started during the date, but ended after the date
                    else if (rec.StartTime.Date == date && rec.EndTime.Date > date)
                    {
                        //get the part that was during the date
                        TimeSpan temp = date.AddDays(1).Subtract(rec.StartTime);
                        total = total.Add(temp);
                    }
                }
            }

            return total;

        }

        public TimeSpan GetHoursWTD(DateTime date)
        {
            int days = date.DayOfWeek - DayOfWeek.Monday;
            DateTime start = date.AddDays(-days);
            DateTime current = start;

            TimeSpan total = new TimeSpan();
            while (current <= date)
            {
                total = total.Add(this.GetHoursForDate(current));
                current = current.AddDays(1);
            }


            return total;
        }

        public Task GetTask(int id)
        {
            Task myTask = null;
            foreach (Task item in TaskList)
            {
                if (item.ID == id)
                {
                    myTask = item;
                    break;
                }
            }
            return myTask;
        }




        //TODO: this is a model function, but it needs to be in the
        //object that holds all Tasks...
        internal void SetTracking(int taskID, bool value)
        {
            if (value == true)
            {
                //stop time on all other tasks
                //and start time on the desired task
                foreach (Task item in TaskList)
                {
                    if (item.ID != taskID && item.IsRunning)
                    {
                        item.StopTime();
                    }
                    else if (item.ID == taskID)
                    {
                        item.StartTime();
                    }
                }


            }
            else
            {
                foreach (Task item in TaskList)
                {
                    if (item.ID == taskID)
                    {
                        item.StopTime();
                        break;
                    }
                }

            }


        }



        public void UpdateTaskTimes()
        {
            try
            {
                if (TaskList != null)
                {
                    foreach (Task item in TaskList)
                    {
                        if (item.IsRunning)
                        {
                            item.UpdateTime();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error updating task times!", ex);
            }
        }


        /// <summary>
        /// Resets the tasks to a stopped state 
        /// Also, gets the max ID of the task list
        internal void InitTaskList()
        {
            //if the application was shutdown before stopping time tacking
            //reset the tasks to a stopped state
            //also, get the max ID of the current list

            maxID = -1;
            foreach (Task item in TaskList)
            {
                item.IsRunning = false;
                item.CurrentTimeRecord = null;
                if (item.ID > maxID)
                {
                    maxID = item.ID;
                }
            }
        }
    }
}
